Thread: I need help for my homework. [so important]

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    As Salem said... break your code in small portions. Here's my implementation with comments for your study:
    Code:
    // cobalt.c
    //
    // Compile and link:
    //
    //  $ gcc -O2 -o cobalt cobalt.c  # add -lm if you use exp().
    //
    
    #include <stdio.h>
    #include <stdlib.h>   // ony for EXIT_* symbols.
    
    // FIX: Don't need this with precalculated constant.
    //#include <math.h>
    
    // Instead of exp(-0.693)/5.272, this C is precalculated printing
    // this expression with 60 decimal places in double precision:
    //
    //  double C = exp(-0.693)/5.272;
    //  printf( "C=%.60f\n", C );
    //
    #define C 0.09485462740815016335904630295772221870720386505126953125 
    
    int main ( void )
    {
      int year;
      double initial;
    
      printf ( "Initial amount of Cobalt (grams)> " );
      fflush( stdout ); // previous print doesn't have a final '\n',
                        // so, it's prudent to flush stdout.
    
      // Check for your inputs!
      if ( scanf ( "%lf", &initial ) != 1 )
      {
    error:
        fprintf( stderr, "Wrong amount.\n" );
        return EXIT_FAILURE;
      }
    
      if ( initial <= 0.0 )
        goto error;
    
      printf ( " Years     Amount      Decay\n" );
      //       "yyyyyy  aaaaaa.aa  dddddd.dd\n"
      //         "% 6d  % 9.2f  % 9.2f"
    
      // Calculate and show decay year by year.
      for ( year = 0; year <= 5; year++ )
      {
        double remain, decay;
    
        decay = C * year;
        remain = initial * ( 1.0 - decay );
    
        // We can format the output directly on the format argument.
        // no need to use ' ' as arguments! See comment above, after printing
        // the 'header'.
        printf ( "% 6d  % 9.2f  % 9.2f\n", year, remain, decay );
      }
    
      return EXIT_SUCCESS;
    }
    []s
    Fred
    Last edited by flp1969; 11-20-2020 at 12:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with this please (IMPORTANT)
    By damha in forum C Programming
    Replies: 11
    Last Post: 04-06-2011, 10:30 AM
  2. Homework guidence, very important
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2002, 07:28 PM
  3. &...how important?
    By CAP in forum C Programming
    Replies: 1
    Last Post: 07-16-2002, 02:02 AM
  4. Is it more important...
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-09-2002, 05:01 AM

Tags for this Thread